package com.finalProject;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class income_jisuuan extends AppCompatActivity {
    String city[]=new String[101] ;
    String income[]=new String[101];
    String order[]=new String[101];
    ListView listView;
    TextView show;
    EditText input;
    AdapterOf_Income adapter;
    List<Income> IncomeList= new ArrayList<Income>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_income_jisuuan);
        Intent out=getIntent();
        Log.i("income传输成功", "onCreate: ");
        city=out.getStringArrayExtra("city");
        order=out.getStringArrayExtra("order");
        income=out.getStringArrayExtra("income");


//        for(int m=0;m<city.length;m++) {
//            String atemp = order[m] + "\t\t" + city[m] + "\t\t" + income[m];
//            Log.i("   ", "onCreate: " + atemp);
//        }

        input=findViewById(R.id.income_jisuan_input);
        show=findViewById(R.id.show_income_order);
        listView=findViewById(R.id.listview_show);

        for(int i=0;i<city.length;i++){

            Income temp_income=new Income(order[i],city[i],income[i]);
            IncomeList.add(temp_income);
        }
        adapter=new AdapterOf_Income( income_jisuuan.this, R.layout.list_item_income, IncomeList);
        listView.setAdapter(adapter);
        input.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String myincomestring = String.valueOf(input.getText());
                if (myincomestring.isEmpty()) {
                    show.setText("请输入");
                }
                else {

                    double myincome = Double.parseDouble(myincomestring);
                    int count = 0;
                    for (int i = 1; i < 101; i++) {
                        double temp = Double.parseDouble(income[i].replace(",", ""));
                        count++;
                        if (temp <= myincome) {
                            break;
                        }


                    }
                    String showstring = "您的收入水平高于" + city[count] + "的平均水平" + income[count] + "\n在全国城市可排到第" + order[count] + "名（如果你是一座城的话）";
                    show.setText(showstring);


                }
            }
        });

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.item_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent start;
        switch (item.getItemId()) {
            case R.id.action_1:
                start=new Intent(income_jisuuan.this,MainActivity.class);
                this.startActivity(start);
                Log.i("", "收入排行榜:跳转完毕 ");
                this.finish();
                break;
            case R.id.action_2:
                Log.i("", "onOptionsItemSelected: 退出成功");
                System.exit(0);
                this.finish();
                break;

        }
        return super.onOptionsItemSelected(item);
    }



}